﻿Public Class Products1
    Dim item As ListViewItem
    Sub listdata()
        If Not rs.EOF Then
            ListView1.Items.Clear()
            rs.MoveFirst()
            Do While Not rs.EOF
                item = ListView1.Items.Add(rs.Fields("productcode").Value)
                item.SubItems.Add(rs.Fields("description").Value)
                item.SubItems.Add("Php " + FormatNumber(rs.Fields("unitprice").Value, 2, True, True, True))

                rs.MoveNext()

            Loop
        Else
            ListView1.Items.Clear()

        End If
        rs.Close()
        rs = Nothing

    End Sub
    Private Sub Products1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Module1.connect()
        rs = New ADODB.Recordset
        rs.Open("select * from products", cn, 3, 2)
        Me.listdata()
        Button2.Enabled = False
        TextBox1.Visible = True
        TextBox5.Visible = False


        RadioButton1.Checked = True
        Button5.Hide()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If TextBox4.Text = "" Or TextBox3.Text = "" Or TextBox1.Text = "" Then
            MsgBox("Supply all fields")
        Else
            If Not (TextBox1.TextLength < 9 And TextBox1.TextLength > 4) Then
                MsgBox("Product Code should be 5 - 8 characters only")
            Else
                If Not IsNumeric(TextBox3.Text) Then
                    MsgBox("Unit Price should be Numeric")
                Else
                    rs = New ADODB.Recordset
                    rs.Open("select * FROM products where productcode = '" & TextBox1.Text & "'", cn, 3, 2)
                    If Not rs.EOF Then
                        MsgBox("This product code has already been added")
                    Else
                        rs = New ADODB.Recordset
                        rs.Open("select * from products", cn, 3, 2)
                        rs.AddNew()

                        rs.Fields("productcode").Value = TextBox1.Text
                        rs.Fields("description").Value = TextBox4.Text
                        rs.Fields("unitprice").Value = TextBox3.Text

                        rs.Update()
                        rs.Close()
                        rs = Nothing
                        MsgBox("Adding Successful")
                        rs = New ADODB.Recordset
                        rs.Open("select * from products", cn, 3, 2)
                        Me.listdata()

                        TextBox5.Text = ""
                        TextBox4.Text = ""
                        TextBox3.Text = ""
                        TextBox1.Text = ""
                    End If
            End If
        End If
        End If
    End Sub

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        rs = New ADODB.Recordset
        rs.Open("SELECT * FROM products WHERE productcode = '" & TextBox5.Text & "'", cn, 3, 2)

        If Not rs.EOF Then
            TextBox4.Text = rs.Fields("description").Value
            TextBox3.Text = rs.Fields("UnitPrice").Value

        End If
        rs.Close()
        rs = Nothing
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        Button1.Enabled = False
        Button2.Enabled = True
        TextBox1.Visible = False
        TextBox5.Visible = True
        TextBox5.Enabled = False

        TextBox4.Enabled = False
        TextBox3.Enabled = False
        TextBox5.Text = ListView1.FocusedItem.Text

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox4.Enabled = True
        TextBox3.Enabled = True
        Button5.Show()
        Button2.Hide()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        MainScreen.Show()
        Me.Close()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Button1.Enabled = True
        Button2.Enabled = False
        TextBox5.Text = ""
        TextBox4.Text = ""
        TextBox3.Text = ""
        TextBox1.Text = ""
        TextBox1.Visible = True
        TextBox5.Visible = False
        Button5.Visible = False
        Button2.Visible = True
        TextBox4.Enabled = True
        TextBox3.Enabled = True

    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
       

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

        If RadioButton1.Checked Then
            rs = New ADODB.Recordset
            rs.Open("SELECT * from products where productcode  like '%" & TextBox2.Text & "%'", cn, 3, 2)
            Me.listdata()
        Else
            If RadioButton2.Checked Then
                rs = New ADODB.Recordset
                rs.Open("SELECT * from products where description  like '%" & TextBox2.Text & "%'", cn, 3, 2)
                Me.listdata()
            End If
            End If
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        TextBox2.Text = ""
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        TextBox2.Text = ""
    End Sub

  
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If TextBox4.Text = "" Or TextBox3.Text = "" Then
            MsgBox("Supply all fields")
        Else
            If Not IsNumeric(TextBox3.Text) Then
                MsgBox("Unit Price should be Numeric")
            Else
                rs = New ADODB.Recordset
                rs.Open("SELECT * from products where productcode = '" & TextBox5.Text & "'", cn, 3, 2)
                rs.Fields("description").Value = TextBox4.Text
                rs.Fields("unitprice").Value = TextBox3.Text
                rs.Update()
                rs.Close()
                rs = Nothing
                MsgBox("Editing Successful")
                rs = New ADODB.Recordset
                rs.Open("select * from products", cn, 3, 2)
                Me.listdata()
            End If
        End If

        Button5.Hide()
        Button2.Show()
        TextBox4.Enabled = False
        TextBox3.Enabled = False
    End Sub

    Private Sub Products1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

    End Sub
End Class